package com.gzdh.dishrecognition.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gzdh.dishrecognition.R;

public class InputDialog extends Dialog {
    private Context mContext;
    private Window window = null;
    TextView msgTv;
    EditText editText;
    TextView okTv;

    public InputDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this.getContext();
        View view = getView();
        setContentView(view);

        //点击dialog以外的空白处是否隐藏
//        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
        //设置窗口显示
        windowDeploy();
    }

    private void initView() {

    }

    private void initData() {

    }


    private void initEvent() {

    }

    private void windowDeploy() {
        window = getWindow();
        window.setGravity(Gravity.CENTER); //设置窗口显示位置
//        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
    }

    private View getView() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        LinearLayout mainLayout = new LinearLayout(mContext);
        mainLayout.setLayoutParams(layoutParams);
        mainLayout.setBackgroundColor(Color.WHITE);
        mainLayout.setOrientation(LinearLayout.VERTICAL);

        //初始化标题
        TextView titleTv = new TextView(mContext);
        titleTv.setText("提示输入框");
        titleTv.setLayoutParams(layoutParams);
        titleTv.setTextColor(Color.BLACK);
        titleTv.setPadding(20, 20, 20, 20);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        mainLayout.addView(titleTv);

        View line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLayout.addView(line);

        mainLayout.addView(getBody());
        return mainLayout;
    }

    private View getBody() {
        LinearLayout mainLinear = new LinearLayout(mContext);
        mainLinear.setOrientation(LinearLayout.VERTICAL);

        StateListDrawable selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, mContext.getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, mContext.getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, mContext.getResources().getDrawable(R.color.white));


        msgTv = new TextView(mContext);
        msgTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        msgTv.setText("标题");
        msgTv.setPadding(15, 15, 15, 15);
        msgTv.setTextColor(Color.BLACK);
        mainLinear.addView(msgTv);

        editText = new EditText(mContext);
        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        editText.setPadding(20, 20, 20, 20);
        mainLinear.addView(editText);

        View line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        LinearLayout bottomLinear = new LinearLayout(mContext);
        bottomLinear.setOrientation(LinearLayout.HORIZONTAL);
        mainLinear.addView(bottomLinear);

        TextView cancelTv = new TextView(mContext);
        cancelTv.setText("取消");
        cancelTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        cancelTv.setTextColor(Color.BLACK);
        cancelTv.setGravity(Gravity.CENTER);
        cancelTv.setPadding(20, 20, 20, 20);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, mContext.getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, mContext.getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, mContext.getResources().getDrawable(R.color.white));
        cancelTv.setBackground(selector);
        cancelTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        cancelTv.setOnClickListener(view -> {
            dismiss();
        });
        bottomLinear.addView(cancelTv);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(1, ViewGroup.LayoutParams.MATCH_PARENT));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        bottomLinear.addView(line);

        okTv = new TextView(mContext);
        okTv.setText("确定");
        okTv.setGravity(Gravity.CENTER);
        okTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        okTv.setTextColor(Color.BLACK);
        okTv.setPadding(20, 20, 20, 20);
        okTv.setTextColor(Color.WHITE);
        okTv.setBackgroundColor(Color.rgb(36, 160, 237));
        okTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        bottomLinear.addView(okTv);


        return mainLinear;
    }

    public TextView getMsgTv() {
        return msgTv;
    }

    public EditText getEditText() {
        return editText;
    }

    public TextView getOkTv() {
        return okTv;
    }
}
